package com.sheliduan.itlearning.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sheliduan.itlearning.admin.model.vos.course.AuditCourseReqVO;
import com.sheliduan.itlearning.admin.model.vos.course.DeleteCourseReqVO;
import com.sheliduan.itlearning.blogger.model.vos.course.FindCourseDetailsRspVO;
import com.sheliduan.itlearning.blogger.model.vos.course.UpdateCourseReqVO;
import com.sheliduan.itlearning.common.model.vos.IdTypeReqVO;
import com.sheliduan.itlearning.admin.service.AdminCourseService;
import com.sheliduan.itlearning.blogger.model.vos.course.FindCoursePageListReqVO;
import com.sheliduan.itlearning.common.aspect.ApiOperationLog;
import com.sheliduan.itlearning.common.domain.dos.*;
import com.sheliduan.itlearning.common.domain.mapper.*;
import com.sheliduan.itlearning.common.utils.PageResponseData;
import com.sheliduan.itlearning.common.utils.ResponseData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 断舍离
 * @date: 2025/3/18 9:09
 * @description: 后台管理员课程控制器
 **/
@RestController
@RequestMapping("/admin")
public class AdminCourseController {

    @Autowired
    private AdminCourseService adminCourseService;

    @Autowired
    private ArticleCourseMapper articleCourseMapper;

    @Autowired
    private VideoCourseMapper videoCourseMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VideoCategoryRelMapper videoCategoryRelMapper;

    @Autowired
    private VideoTagRelMapper videoTagRelMapper;

    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;


    /*
     * 分页查询非草稿状态课程
     * */
    @PostMapping("/course/pageList")
    @ApiOperationLog(description = "非草稿状态课程数据分页查询")
    public PageResponseData findCoursePageList(@RequestBody @Validated FindCoursePageListReqVO findCoursePageListReqVO){
        if(findCoursePageListReqVO.getType() == 0) {
            return adminCourseService.findArticleCoursePageList(findCoursePageListReqVO);
        } else {
            return adminCourseService.findVideoCoursePageList(findCoursePageListReqVO);
        }
    }

    /*
     * 审核课程
     * */
    @PostMapping("/course/audit")
    @ApiOperationLog(description = "审核课程")
    @Transactional
    public ResponseData auditCourse(@RequestBody @Validated AuditCourseReqVO auditCourseReqVO) {
        if(auditCourseReqVO.getType() == 0) {
            // 说明该课程审核通过, 需要更新对应分类/标签下的课程总数
            updateTotal(auditCourseReqVO.getStatus(), auditCourseReqVO.getType(), auditCourseReqVO.getId());
            return ResponseData.success(articleCourseMapper.updateCourseStatus(auditCourseReqVO.getId(), auditCourseReqVO.getStatus()));
        }else {
            // 说明该课程审核通过, 需要更新对应分类/标签下的课程总数
            updateTotal(auditCourseReqVO.getStatus(), auditCourseReqVO.getType(), auditCourseReqVO.getId());
            return ResponseData.success(videoCourseMapper.updateCourseStatus(auditCourseReqVO.getId(), auditCourseReqVO.getStatus()));
        }
    }


    /*
     * 删除课程
     * */
    @PostMapping("/course/delete")
    @ApiOperationLog(description = "根据id删除课程")
    public ResponseData deleteCourse(@RequestBody @Validated DeleteCourseReqVO reqVO) {
        return adminCourseService.deleteCourse(reqVO);
    }

    /*
     * 根据id和type查找课程
     * */
    @PostMapping("/course/selectById")
    @ApiOperationLog(description = "根据id和type查找课程")
    public ResponseData selectById(@RequestBody @Validated IdTypeReqVO idTypeReqVO) {
        return adminCourseService.selectById(idTypeReqVO);
    }

    /*
     * 更新对应分类、标签下的文章/视频课程总数
     * */
    private void updateTotal(Integer status, Integer type, Integer id) {
        if(type == 0 && status == 2) {
            // 更新对应分类、标签下的文章课程总数
            LambdaQueryWrapper<ArticleCategoryRelDO> wrapperCategory = Wrappers.lambdaQuery();
            wrapperCategory.eq(ArticleCategoryRelDO::getArticleId, id);
            // 该文章对应的分类
            CategoryDO categoryDO = categoryMapper.selectById(articleCategoryRelMapper.selectOne(wrapperCategory).getCategoryId());
            LambdaQueryWrapper<ArticleTagRelDO> wrapperTag = Wrappers.lambdaQuery();
            wrapperTag.eq(ArticleTagRelDO::getArticleId, id);
            // 该文章对应的标签
            TagDO tagDO = tagMapper.selectById(articleTagRelMapper.selectOne(wrapperTag).getTagId());
            categoryDO.setArticleTotal(categoryDO.getArticleTotal() + 1);
            categoryMapper.updateById(categoryDO);
            tagDO.setArticleTotal(tagDO.getArticleTotal() + 1);
        }else if(type == 1 && status == 2){
            // 更新对应分类、标签下的视频课程总数
            LambdaQueryWrapper<VideoCategoryRelDO> wrapperCategory = Wrappers.lambdaQuery();
            wrapperCategory.eq(VideoCategoryRelDO::getVideoId, id);
            // 该视频对应的分类
            CategoryDO categoryDO = categoryMapper.selectById(videoCategoryRelMapper.selectOne(wrapperCategory).getCategoryId());
            LambdaQueryWrapper<VideoTagRelDO> wrapperTag = Wrappers.lambdaQuery();
            wrapperTag.eq(VideoTagRelDO::getVideoId, id);
            // 该视频对应的标签
            TagDO tagDO = tagMapper.selectById(videoTagRelMapper.selectOne(wrapperTag).getTagId());
            categoryDO.setArticleTotal(categoryDO.getArticleTotal() + 1);
            categoryMapper.updateById(categoryDO);
            tagDO.setArticleTotal(tagDO.getArticleTotal() + 1);
            tagMapper.updateById(tagDO);
        }
    }

    /*
     * 修改课程
     * */
    @PostMapping("/course/update")
    @ApiOperationLog(description = "修改课程")
    public ResponseData updateCourse(@RequestBody @Validated UpdateCourseReqVO updateCourseReqVO){
        if(updateCourseReqVO.getType() == 0) {
            return adminCourseService.updateArticleCourse(updateCourseReqVO);
        }else {
            return adminCourseService.updateVideoCourse(updateCourseReqVO);
        }
    }




}
